package qq2564874169.tuaotuao.fx.orm.where;


import qq2564874169.tuaotuao.fx.Action1T;
import qq2564874169.tuaotuao.fx.Dynamic;
import qq2564874169.tuaotuao.fx.orm.query.ForUpdate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class QueryWhere<T> extends ExecuteWhere<T> {
    public QueryWhere(Class<T> clazz) {
        super(clazz);
    }

    public String fields() {
        return "*";
    }

    public Integer skip() {
        return null;
    }

    public T asc() {
        return null;
    }

    public T desc() {
        return null;
    }

    public ForUpdate forUpdate() {
        return ForUpdate.NoLock;
    }

    @Override
    public QueryWhere<T> clone(Action1T<Dynamic> action) {
        Class<T> table = getTable();
        Map<String, Object> map = new HashMap<>(24);
        map.put("allowEmptyCondition", allowEmptyCondition());
        map.put("splitCondition", splitCondition());
        map.put("fields", fields());
        map.put("eq", eq());
        map.put("neq", neq());
        map.put("less", less());
        map.put("greater", greater());
        map.put("greaterEq", greaterEq());
        map.put("lessEq", lessEq());
        map.put("like", like());
        map.put("notLike", notLike());
        map.put("isNull", isNull());
        map.put("isNotNull", isNotNull());
        map.put("in", in());
        map.put("inOrNull", inOrNull());
        map.put("skip", skip());
        map.put("take", take());
        map.put("desc", desc());
        map.put("asc", asc());
        map.put("notIn", notIn());
        map.put("notInOrNull", notInOrNull());
        map.put("index", index());
        map.put("forUpdate", forUpdate());
        Dynamic dm = new Dynamic(map);
        action.execute(dm);
        return new QueryWhere<T>(table) {
            private Dynamic pm = dm;

            @Override
            public ForUpdate forUpdate() {
                return (ForUpdate) pm.get("forUpdate");
            }

            @Override
            public List<T> notIn() {
                return (List<T>) pm.get("notIn");
            }

            @Override
            public List<T> notInOrNull() {
                return (List<T>) pm.get("notInOrNull");
            }

            @Override
            public String index() {
                return pm.getString("index");
            }

            @Override
            public boolean allowEmptyCondition() {
                return pm.getBool("allowEmptyCondition");
            }

            @Override
            public String splitCondition() {
                return pm.getString("splitCondition");
            }

            @Override
            public String fields() {
                return pm.getString("fields");
            }

            @Override
            public T eq() {
                return (T) pm.get("eq");
            }

            @Override
            public T neq() {
                return (T) pm.get("neq");
            }

            @Override
            public T less() {
                return (T) pm.get("less");
            }

            @Override
            public T greater() {
                return (T) pm.get("greater");
            }

            @Override
            public T lessEq() {
                return (T) pm.get("lessEq");
            }

            @Override
            public T greaterEq() {
                return (T) pm.get("greaterEq");
            }

            @Override
            public T like() {
                return (T) pm.get("like");
            }

            @Override
            public T notLike() {
                return (T) pm.get("notLike");
            }

            @Override
            public T isNull() {
                return (T) pm.get("isNull");
            }

            @Override
            public T isNotNull() {
                return (T) pm.get("isNotNull");
            }

            @Override
            public List<T> in() {
                return (List<T>) pm.get("in");
            }

            @Override
            public List<T> inOrNull() {
                return (List<T>) pm.get("inOrNull");
            }

            @Override
            public Integer skip() {
                return pm.getInt("skip");
            }

            @Override
            public Integer take() {
                return pm.getInt("take");
            }

            @Override
            public T desc() {
                return (T) pm.get("desc");
            }

            @Override
            public T asc() {
                return (T) pm.get("asc");
            }
        };
    }
}
